// ScribbleDoc.cpp : implementation of the CScribbleDoc class
//

#include "stdafx.h"
#include "Scribble.h"
#include"PenWidthDlg.h"
#include "ScribbleDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc

IMPLEMENT_DYNCREATE(CScribbleDoc, CDocument)

BEGIN_MESSAGE_MAP(CScribbleDoc, CDocument)
	//{{AFX_MSG_MAP(CScribbleDoc)
	ON_COMMAND(ID_EDIT_CLEARALL, OnEditClearall)
	ON_COMMAND(ID_PEN_THICKLINE, OnPenThickline)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CLEARALL, OnUpdateEditClearall)
	ON_UPDATE_COMMAND_UI(ID_PEN_THICKLINE, OnUpdatePenThickline)
	ON_COMMAND(ID_PEN_PENWIDTH, OnPenPenwidth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc construction/destruction

CScribbleDoc::CScribbleDoc()
{
	// TODO: add one-time construction code here

}

CScribbleDoc::~CScribbleDoc()
{
}

BOOL CScribbleDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	InitDocument();

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc serialization

void CScribbleDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar<<m_sizeDoc;
	}
	else
	{
		ar>>m_sizeDoc;
		// TODO: add loading code here
	}
	m_strokeList.Serialize(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc diagnostics

#ifdef _DEBUG
void CScribbleDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CScribbleDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
	dc.SetDepth(2);
	dc<<m_strokeList;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc commands
void CScribbleDoc::InitDocument()
{
	m_bThickPen=FALSE;
//	m_nPenWidth=2;
	m_nThinWidth=2;
	m_nThickWidth=5;
	ReplacePen();
	m_sizeDoc=CSize(800,900);
//	m_penCur.CreatePen(PS_SOLID,m_nPenWidth,RGB(0,0,0));
}

BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	InitDocument();
	
	// TODO: Add your specialized creation code here
	
	return TRUE;
}

void CScribbleDoc::DeleteContents() 
{
#ifdef _DEBUG
	Dump(afxDump);
#endif
	// TODO: Add your specialized code here and/or call the base class
	while(!m_strokeList.IsEmpty())
		delete m_strokeList.RemoveHead();

	CDocument::DeleteContents();
}
CStroke *CScribbleDoc::NewStroke()
{
	CStroke *pStrokeItem=new CStroke(m_nPenWidth);
	m_strokeList.AddTail(pStrokeItem);
	SetModifiedFlag();
	return pStrokeItem;
}

void CScribbleDoc::OnEditClearall() 
{
	DeleteContents();
	SetModifiedFlag();
	UpdateAllViews(NULL );
}
void CScribbleDoc::ReplacePen()
{
	m_nPenWidth=m_bThickPen?m_nThickWidth:m_nThinWidth;
	m_penCur.DeleteObject();
	m_penCur.CreatePen(PS_SOLID,m_nPenWidth,RGB(0,0,0));
}

void CScribbleDoc::OnPenThickline() 
{

	m_bThickPen=!m_bThickPen;
	ReplacePen();
}

void CScribbleDoc::OnUpdateEditClearall(CCmdUI* pCmdUI) 
{

	pCmdUI->Enable(!m_strokeList.IsEmpty());
}

void CScribbleDoc::OnUpdatePenThickline(CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck(m_bThickPen);
}

void CScribbleDoc::OnPenPenwidth() 
{

	CPenWidthDlg dlg;
	dlg.m_nThickWidth=m_nThickWidth;
	dlg.m_nThinWidth=m_nThinWidth;
	if(dlg.DoModal()==IDOK)
	{
		m_nThinWidth=dlg.m_nThinWidth;
        m_nThickWidth=dlg.m_nThickWidth;
		ReplacePen();
	}
}

BOOL CScribbleDoc::SaveModified() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDocument::SaveModified();
}
